%macro merge_returns;

	
	
	/*
	The tasks are:
		Determine the maximum amount of 1099-G state income tax refund.
		Long-term carryforwards.
		
	*/
		
	
	proc printto log = "&logdir/merge_returns.txt" new;
	run;
	
	/* this will NOT be unique by prim_tin_x */
	proc sort data = rlib.filing_status(where=(prim_tin_x ~= .))	
		out = filing_status;
		by prim_tin_x;
	run;
	
	
	
	/* merge all the tax return data together */
	
	
	
		data pre_agg;
			merge filing_status(in=a) rlib.f1040
				rlib.itemized(in=b)  rlib.sched_d ;
			by prim_tin_x;
			if a;
		
			array numvars(6) st_sched_d lt_sched_d itemized state_incm_tax_ded adl_st eitc;
			do i = 1 to 6;
				if numvars(i) = . then numvars(i) = 0;
			end;	
			
			if filing_status = 3 then current = 1500;
			else current = 3000;
			
			tot_cf = max(0, -(st_sched_d + lt_sched_d)-current);
			st_loss_cf = max(0, min(tot_cf, -st_sched_d));
			lt_loss_cf = tot_cf - st_loss_cf;
			
			/* compute maximum amount of state income tax refund */
			
			/* ignoring issues related to dependent filers */
			
			%if &base_yr = 2019 %then %do; /* 2018 values of standard deduction */
				%let sd1 = 6000;
				%let sd2 = 9000;
				%let sd4 = 12000;
			%end;
			
			sd = &sd1;
			if filing_status = 2 then sd = &sd2;
			if filing_status = 4 then sd = &sd4;
			st_incm_tax_max = min(state_incm_tax_ded,
				max(0, itemized - sd - adl_sd));
				
				
			/* Other housekeeping */
			lag_itemizer = b;
			lag_cg = st_sched_d + lt_sched_d;
			
				
			keep filing_status prim_tin sec_tin st_loss_cf lt_loss_cf st_incm_tax_max lag_:;	
		run;
		
		/* aggregate by prim_tin, sec_tin */
		proc means data = pre_agg nway noprint;
			class prim_tin sec_tin;
			output out = by_tu(drop=_type_ _freq_)
				sum(st_loss_cf lt_loss_cf st_incm_tax_max lag_eitc
					lag_state_ref lag_ui_inc lag_all_ss lag_gr_pens
					lag_intincm lag_divincm lag_sch1_incm lag_cg lag_agi) =
				st_loss_cf lt_loss_cf st_incm_tax_max lag_eitc
					lag_state_ref lag_ui_inc lag_all_ss lag_gr_pens
					lag_intincm lag_divincm lag_sch1_incm lag_cg lag_agi
				max(lag_inelig_ben lag_itemizer) = 
					lag_inelig_ben lag_itemizer
				;
		run;
		
		
		/* filing status */
		proc sort data = pre_agg(keep=prim_tin sec_tin filing_status) out = nodups nodupkey;
			by prim_tin sec_tin filing_status;
		run;
		proc means data = nodups nway noprint;
			class prim_tin sec_tin;
			output out = nfs(drop=_type_ _freq_)
				n(filing_status) = nfs;
		run;
		
		/* pick one filing status arbitrarily */
		proc sort data = nodups nodupkey;
			by prim_tin sec_tin;
		run;
		
		/* but flag if there were multiple filing statuses */
		data rlib.tax_returns;
			merge by_tu nodups nfs(where=(nfs>1) in=b) ;
			by prim_tin sec_tin;
			mult_fs = nfs > 1;
			drop nfs;
		run;
		
		
		
			
		
		
	
	
	
	
	

	
	proc printto;
	run;
	
	
%mend;

